package cn.pangza.user.controller;

import cn.pangza.mybatisplus.entity.PageResult;
import cn.pangza.redis.utils.RedisShareLockUtil;
import cn.pangza.redis.utils.RedisUtil;
import cn.pangza.user.dto.CreateUserDto;
import cn.pangza.user.dto.UpdateUserDto;
import cn.pangza.user.entity.UserEntity;
import cn.pangza.user.excel.DemoExcelExport;
import cn.pangza.user.service.UserService;
import cn.pangza.mybatisplus.entity.PageRequest;
import cn.pangza.web.exception.BusinessException;
import cn.pangza.web.utils.ResponseData;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "用户接口")
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    RedisShareLockUtil redisShareLockUtil;

    @Autowired
    DemoExcelExport demoExcelExport;

    @PostMapping()
    @ApiOperation(value = "新增用户", notes = "用户数据")
//    @DynamicParameters(name = "CreateOrderHashMapModel",properties = {
//            @DynamicParameter(name = "",value = "注解id",example = "X000111",required = true,dataTypeClass = Integer.class),
//            @DynamicParameter(name = "name3",value = "订单编号-gson"),
//            @DynamicParameter(name = "name1",value = "订单编号1-gson"),
//    })
    public ResponseData<Object> createUser(@RequestBody CreateUserDto createUserDto) {
       userService.createUser(createUserDto);
        return ResponseData.success();
    }

    @PutMapping()
    public ResponseData<Object> updateUser(@RequestBody UpdateUserDto updateUserDto) throws BusinessException {
        throw new BusinessException("修改失败");
    }

    @DeleteMapping("/{id}")
    public ResponseData<Object> deleteUser(@PathVariable("id") Integer id) {
        userService.removeById(id);
        return ResponseData.success();
    }

    @GetMapping()
    public ResponseData<List<UserEntity>> getUserList(){
        return ResponseData.success(userService.list());
    }

    @PostMapping("/page")
    public ResponseData<PageResult<UserEntity>> getUserPage(@RequestBody PageRequest<UserEntity> pageRequest) {
        return ResponseData.success(userService.getUserPage(pageRequest));
    }

    @GetMapping("/redis/set")
    public ResponseData<Object> setRedis() {
        redisUtil.set("name", "pangza胖砸1");
        return ResponseData.success();
    }

    @GetMapping("/redis/get")
    public ResponseData<String> getRedis() {
        return ResponseData.success(redisUtil.get("name").toString());
    }

    @GetMapping("/redis/lock/set")
    public ResponseData<String> setLockRedis() {
        boolean pangza = redisShareLockUtil.lock("pangza", "123123", 100000L);
        System.err.println(pangza);
        return ResponseData.success();
    }
    @GetMapping("/redis/lock/get")
    public ResponseData<String> getLockRedis() {
        return ResponseData.success(redisUtil.get("name").toString());
    }

    @GetMapping("/test/log")
    public ResponseData<Object> testLog() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            log.info("这是{}日志打印", i);
        }
        long endTime = System.currentTimeMillis();
        log.info("打印耗时{}", endTime - startTime);
        return ResponseData.success();
    };

    @GetMapping("/test/export")
    public void exportData() {
       demoExcelExport.exportData("用户表", new HashMap<>());
    }

}
